<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of content
 *
 * @author dmitry
 */
require_once 'app/access/db.php';
//=======================================
class Models_Content extends Access_Db {
    //put your code here
    public function getLastAdded()
    {
        $result = $this->executeQuery('
            SELECT ID,name,added 
            FROM yyProducts 
            WHERE visible=1 ORDER BY added DESC limit 10') ;
        while ($row = mysql_fetch_assoc($result))
        {
            $product[$row['ID']] = $row['name'] ;
        }
        return $product ;
    }
    
    public function getCategory($id)
    {
        
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT ID, name
            FROM yyProducts
            WHERE visible=1 and categoryID='. $id .' ORDER BY name ASC limit 10') ;
        
            while ($row = mysql_fetch_assoc($result))
            {
                $product[$row['ID']] = $row['name'] ;
            }
            if (!isset ($product)) {
                    die ('<h3><p>Sorry, not found...</p><a href="/' . $this->path 
                            . '">>>>Back.<<<</a></h3>') ;
                }
            return $product ;
         
    }
    
    public function getProduct($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT yyProducts.ID,yyProducts.name,yyProducts.ratio,
            yyProducts.price,yyProducts.votes,yyPictures.name as picname,yyPictures.path
            FROM yyProducts,yyPictures
            WHERE yyPictures.main=1
            AND yyProducts.ID=yyPictures.productID
            AND yyProducts.visible=1 
            AND yyProducts.ID=' . $id) ;
        return mysql_fetch_assoc($result) ;
    }
    
    public function getPictures($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT name,path
            FROM yyPictures
            WHERE main=0
            AND productID=' . $id) ;
        while ($row = mysql_fetch_array($result))
        {
            $pictures[$row['name']] = $row['path'] ;
        }
        if (isset ($pictures))
        {
            return $pictures;
        } return false;
        
    }

    public function getProperty($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT yyProductProperties.value,yyProperties.name
            FROM yyProductProperties,yyProperties
            WHERE yyProductProperties.propertyID=yyProperties.ID 
            AND yyProductProperties.productID=' . $id) ;
        while ($row = mysql_fetch_array($result))
        {
            $property[$row['name']] = $row['value'] ;
        }
        return $property ;
    }

    public function getShortData($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT yyProducts.ID,yyProducts.ratio,yyProducts.price,yyProducts.uri,
            yyPictures.name,yyPictures.path
            FROM yyProducts,yyPictures
            WHERE yyProducts.ID='.$id.' and yyPictures.productID='. $id .' and yyPictures.main=1') ;
        return mysql_fetch_array($result) ;
    }
    
    public function getDiscuss($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery('
            SELECT ID, userID
            FROM yyProductDiscussion
            WHERE productID=' . $id) ;
        while ($row = mysql_fetch_array($result))
        {
            $discuss[$row['ID']] = $row['userID'] ; 
        }
        if (isset ($discuss)) {
        return $discuss ; } return false ;
    }
    
    public function addComment($commentID,$userID,$productID,$commentContent)
    {
        settype($commentID, 'integer') ;
        settype($userID, 'integer') ;
        settype($productID, 'integer') ;
        $this->executeQuery("
            INSERT INTO yyProductDiscussion
            (ID,secondID,userID,productID,content,added,visible)
            VALUES (NULL,$commentID,$userID,$productID,'$commentContent',NOW(),1)") ;
    }
    
    public function showCommentForm($id,$userID,$productID)
    {
        echo '<div class="commentform">
                <form action="/' . $this->path . 'products/addcomment/' . $productID . '" method="POST">
                    <input type="hidden" name="id" value="' . $id . '">
                    <input type="hidden" name="user" value="' . $userID . '">
                    <input type="hidden" name="product" value="' . $productID . '">
                    <textarea class="elm1" name="comment" rows="15" cols="80">
                    </textarea>
                    <input type="submit" name="submit" value="commit">
                    <input type="reset" name="reset" value="reset">
                </form>
              </div>' ;
    }
    
    public function showComments($discuss,$goodsID)
    {
        if (!$discuss) {return false ;}
        foreach ($discuss as $id => $userID)
        {
            $result = $this->executeQuery('
                SELECT yyProductDiscussion.content,yyProductDiscussion.added,
                yyProductDiscussion.secondID,yyUsers.login
                FROM yyProductDiscussion,yyUsers
                WHERE yyProductDiscussion.visible=1
                AND yyProductDiscussion.ID=' . $id . '
                AND yyUsers.ID=' . $userID) ;
            $row = mysql_fetch_assoc($result) ;
            if ($row['secondID'] == 0)
            {
                echo '<div class="maincomment">
                        <div class="headcomment">
                            <div class="username"> [' . $row['login'] . ']</div>
                            <div class="date"> added : [' . $row['added'] . ']</div>
                        </div>
                        <div class="commentcontent">' . $row['content'] . '</div>
                        <div class="footcomment">
                            <form action="/' . $this->path . 'products/goods/' . $goodsID . '" method="POST">
                              <input type="hidden" name="id" value="' . $id . '">
                              <input type="submit" value="answer">  
                            </form>
                        </div>
                      </div>' ;
                $exResult = $this->executeQuery('
                SELECT yyProductDiscussion.content,yyProductDiscussion.added,
                yyProductDiscussion.secondID,yyUsers.login
                FROM yyProductDiscussion,yyUsers
                WHERE yyProductDiscussion.visible=1
                AND yyProductDiscussion.secondID=' . $id . '
                AND yyUsers.ID=' . $userID . '
                ORDER BY yyProductDiscussion.added ASC') ;
                while ($ex = mysql_fetch_assoc($exResult))
                {
                    echo '<div class="subcomment">
                        <div class="headcomment">
                            <div class="username">[' . $ex['login'] . ']</div>
                            <div class="date"> added : [' . $ex['added'] . ']</div>
                        </div>
                        <div class="commentcontent">' . $ex['content'] . '</div>
                      </div>' ;
                }
                if (isset ($_POST['id']) && $_POST['id'] == $id)
                {
                    echo '<div class="subcomment">' ;
                    $this->showCommentForm($id, $userID, $goodsID) ;
                    echo '</div>' ;
                }
            } 
        }
        
        echo '<div class="maincomment">' ;
        $this->showCommentForm(0, 1, $goodsID) ;
        echo '</div>' ;
    }


    public function showProducts($content)
    {        
        foreach ($content as $id => $name)
        {
            $data = $this->getShortData($id) ;
            echo '<div class="unit">
                       <div>
                       <div class="image">
                            <img src="/' . $this->path . 'public/pic/' .
                                $data['path'] . $data['name'] .'">
                       </div>
                       <div class="unitname">
                            <a href="/' . $this->path . 'products/goods/' .
                            $data['ID'] . '">' .
                            $name .'</a>
                       </div>
                  </div>
                       <div class="price">' .
                           $data['price']
                       . '</div></div>' ;
        }
    }
    
    public function showGoods($id)
    {
        $product = $this->getProduct($id) ;
        $pics = $this->getPictures($id) ;
        $info = $this->getProperty($id) ;
        echo '<div id="goods">
                <div class="goodsname">' . $product['name'] . '</div>
                <div class="goodsprice">' . $product['price'] . '</div>
                <div id="pics">
                    <div id="mainpic">
                        <img src="/' . $this->path . 'public/pic/' .
                        $product['path'] . $product['picname'] . '">
                    </div>
                    <div id="secpic">' ;
        if ($pics)
        {
            foreach ($pics as $name => $path)
            {
                echo '<div class="goodsimage">
                        <a href="/' . $this->path . 'public/pic/' .
                        $path . $name . '" rel="lightbox[' . $id . ']" title="' . $name . '">
                            <img src="/' . $this->path . 'public/pic/' .
                        $path . $name . '"></a>
                      </div>' ;
            }
        }
        echo '</div></div>
              <div id="goodsinfo">' ;
        foreach ($info as $name => $value)
        {
            echo '<div class="line">
                    <div class="propertyname">' . $name . '</div>
                    <div class="propertyvalue">' . $value . '</div>
                  </div>' ;
        }
        echo '</div>
              <div id="discuss">' ;
        $this->showComments($this->getDiscuss($id),$id) ;
        echo '</div></div>' ;
        
        
    }
}

?>
